package com.example.myapplication7;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ViewPager2 viewPager2;
private LinearLayout llchat,llcontact,llfind,llprofile;
private ImageView ivchat,ivcontact,ivfind,ivprofile,ivcurrent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
initPager();
initTabView();
    }
void initTabView(){
        llchat=findViewById(R.id.id_tab_weixin);
        llchat.setOnClickListener(this);
        llcontact=findViewById(R.id.id_tab_contact);
    llcontact.setOnClickListener(this);
        llfind=findViewById(R.id.id_tab_find);
    llfind.setOnClickListener(this);
        llprofile=findViewById(R.id.id_tab_profile);
        llprofile.setOnClickListener(this);

ivchat=findViewById(R.id.tab_iv_weixin);
ivcontact=findViewById(R.id.tab_iv_contact);
ivfind=findViewById(R.id.tab_iv_find);
ivprofile=findViewById(R.id.tab_iv_profile);

ivchat.setSelected(true);

ivcurrent=ivchat;

}
    void initPager()
    {
        viewPager2=findViewById(R.id.viewpager);
        ArrayList<Fragment> fragments=new ArrayList<>();
        fragments.add(BlankFragment.newInstance("小梁"));
        fragments.add(BlankFragment.newInstance("一定"));

        fragments.add(BlankFragment.newInstance("要要要"));
        fragments.add(BlankFragment.newInstance("加油"));

FragementAdapter fragementAdapter=new FragementAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
viewPager2.setAdapter(fragementAdapter);
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        super.onPageScrolled(position, positionOffset, positionOffsetPixels);
    }

    @Override
    public void onPageSelected(int position) {
        super.onPageSelected(position);
        changeTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        super.onPageScrollStateChanged(state);
    }
});


    }
@SuppressLint("NonConstantResourceId")
void changeTab(int position)
{ivcurrent.setSelected(false);

    if(position==R.id.id_tab_weixin) {

        ivchat.setSelected(true);
        ivcurrent=ivchat;
        viewPager2.setCurrentItem( 0);
    }
    else if(position==R.id.id_tab_contact)
    {
        ivcontact.setSelected(true);
        ivcurrent=ivcontact;
        viewPager2.setCurrentItem(1);
    }
    else if(position==R.id.id_tab_find)
    {
        ivfind.setSelected(true);
        ivcurrent=ivfind;
        viewPager2.setCurrentItem(2);
    }
        else if(position==R.id.id_tab_profile) {
        ivprofile.setSelected(true);
        ivcurrent=ivprofile;
        viewPager2.setCurrentItem(3);
    }

else{
    switch(position) {


        case 0:
            ivchat.setSelected(true);
            ivcurrent = ivchat;

            break;
        case 1:
            ivcontact.setSelected(true);
            ivcurrent = ivcontact;

            break;

        case 2:
            ivfind.setSelected(true);
            ivcurrent = ivfind;
            break;

        case 3:
            ivprofile.setSelected(true);
            ivcurrent = ivprofile;
            break;

    }
}

}

    @Override
    public void onClick(View view) {


changeTab(view.getId());
    }
}